package com.example.satoken.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import com.example.satoken.annotion.BaseResponse;
import com.example.satoken.constants.UserConstants;
import com.example.satoken.model.LoginBody;
import com.example.satoken.model.RegisterBody;
import com.example.satoken.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@BaseResponse
@Slf4j
@RestController
@RequestMapping("/user")
@AllArgsConstructor
@Api(tags = "UserController", value = "用户信息")
public class UserController {

    private final IUserService iUserService;

    /**
     * 登录请求
     *
     * @param loginBody
     * @return
     */

    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "登录")
    public Dict doLogin(@Validated @RequestBody LoginBody loginBody) {
        Dict dict = Dict.create();
        String token = iUserService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getType());
        return dict.set(UserConstants.TOKEN, token);
    }


    /**
     * 注册请求
     *
     * @param registerBody
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册", notes = "注册")
    public String register(@Validated @RequestBody RegisterBody registerBody) {
        iUserService.register(registerBody);
        return "SUCCESS";
    }


    /**
     * 登录是否
     *
     * @return
     */
    @GetMapping("/isLogin")
    @ApiOperation(value = "登录是否", notes = "登录是否")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    /**
     * 登录
     *
     * @param loginId
     * @return
     */
    @GetMapping("/logout")
    @ApiOperation("退出登录")
    public String logout(String loginId) {
        StpUtil.logout();
        return "SUCCESS";
    }


    /**
     * 当前时间信息
     *
     * @return
     */
    @GetMapping("/getTime")
    @ApiOperation(value = "查询当前时间", notes = "查询当前时间")
    public String tokenInfo() {
        return DateUtil.now();
    }

}
